/**
 * Author :tianhongyu
 * Date : 2018/4/28 16:30
 */
package com.tian.core.config;

import com.tian.core.page.PageInterceptor;
import com.tian.core.page.PageResultInterceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import javax.sql.DataSource;

/**
 * @author tianhong yu
 * @date 2018/4/2816:30
 */
@Configuration
public class MybatisConfig {

    @Bean
    public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource) throws Exception {
        SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
        factory.setDataSource(dataSource);
        //model存储路径
        factory.setTypeAliasesPackage("com.tian.source.model");
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        //xml路径
        factory.setMapperLocations(resolver.getResources("classpath:mapper/*Mapper.xml"));
        factory.setPlugins(new org.apache.ibatis.plugin.Interceptor[]{new PageInterceptor(),new PageResultInterceptor()});
        return factory.getObject();
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBean");
        //mapper接口存储路径
        mapperScannerConfigurer.setBasePackage("com.tian.source.mapper");
        return mapperScannerConfigurer;
    }
}
